package com.atguigu.queue;
//数组模拟环形队列
public class CircleArrayQueueDemo {
    public static void main(String[] args) {

    }
}

class CircleArrayQueue {
    //队列最大容量maxSize
    private int maxSize;
    //队列头
    private int front;
    //队列尾
    private int rear;
    //数组容器
    int[] arr;

    //队列构造器
    public CircleArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];//队列容量
        //环形队列下面参数改变
        front = 0; //指向队列头部，分析出front是指向队列头位置
        rear = 0; //指向队列尾，指向队列尾的后一个数据
    }

    //判断队列是否满
    public boolean isFull() {
        //判断满依据：(rear + 1) % maxSize = front
        return (rear + 1) % maxSize == front;
    }

    //判断队列是否空
    public boolean isEmpty() {
        return rear == front;
    }

    //队列添加数据
    public void addQueue(int n) {
        //先判断队列是否满
        if (isFull()) {
            System.out.println("队列已满，不能再添加数据");
            return;
        }
        arr[rear] = n; //直接修改
        rear = (rear + 1) % maxSize ;//将rear后移，这里必须考虑取模
    }

    //获取队列数据
    public int getQueue() {
        //先判断是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列为空，无法取数据");
        }
        //1.将要取的数据放入临时变量
        //2.front后移,考虑取模
        //3.返回临时变量的数据
        int value = arr[front] ;
        front = (front + 1) % maxSize ;
        return value ;
    }

    //显示队列所有数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列是空的，没有数据");
            return;
        }
        for (int i = front; i < size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    //求出当前队列有效数据个数
    public int size() {
        return (rear + maxSize - front) % maxSize ;
    }

    //显示队列的头数据，注意不是取数据
    public int headQueue() {
        //非空判断
        if (isEmpty()) {
            throw new RuntimeException("队列是空的，没有数据");
        }
        return arr[front];
    }
}
